package com.binvshe.binvshe.mine.view;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.binvshe.binvshe.R;
import com.binvshe.binvshe.common.http.DataRequestListener;
import com.binvshe.binvshe.common.http.ListDataRequestListener;
import com.binvshe.binvshe.common.http.MyHttpRequest;
import com.binvshe.binvshe.common.http.UrlCenter;
import com.binvshe.binvshe.common.image.IML;
import com.binvshe.binvshe.mine.FansActivity;
import com.binvshe.binvshe.mine.FollowState;
import com.binvshe.binvshe.mine.follow.bean.FollowPersonBean;
import com.binvshe.binvshe.mine.usercenter.UserCenterActivity;
import com.pachong.android.baseuicomponent.adapter.RecyclerViewBaseAdapter;
import com.pachong.android.baseuicomponent.fragment.SwipeListFragment;
import com.pachong.android.framework.httprequest.UrlParams;
import com.pachong.android.frameworkbase.utils.EasyToast;

import butterknife.Bind;
import butterknife.ButterKnife;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * FileName: FansFragment
 *
 * @CretedBy : zofnk
 * @CreateDate: 2016/9/24 10:52
 * @E-mail: zofnk@vip.qq.com
 * @RevisionsDate：
 * @ModifyNotes：
 */
public class FansFragment extends SwipeListFragment {

    private int mPage = 0;

    public static FansFragment newInstance() {
        FansFragment fragment = new FansFragment();
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        startLoading();
        return view;
    }

    @Override
    public RecyclerViewBaseAdapter createAdapter() {
        return new FollowPersonListAdapter(getActivity());
    }

    @Override
    public void onStartRefreshing() {
        super.onStartRefreshing();
        getData().clear();
        mPage = 0;
        startLoading();
    }

    @Override
    public void onResume() {
        super.onResume();
        onStartRefreshing();
    }

    @Override
    public void onStartLoading() {
        getFansListData();
    }

    private ListDataRequestListener<FollowPersonBean> mResponceListener = new ListDataRequestListener<>(this, FollowPersonBean.class, true);

    void getFansListData() {
        if (mPage == 0 || mResponceListener.getResponseDataParser().hasMore()) {
            mPage++;
            MyHttpRequest request = new MyHttpRequest(getContext());
            UrlParams urlParams = new UrlParams();
            request.get(UrlCenter.USER_FANS, urlParams, mResponceListener);
        }
    }

    public class FollowPersonListAdapter extends RecyclerViewBaseAdapter<FollowPersonBean> {

        public FollowPersonListAdapter(Context con) {
            super(con);
        }

        @Override
        public RecyclerView.ViewHolder onCreateHolder(ViewGroup parent, int viewType) {
            return new VH(setAdapterViewItem(parent, R.layout.item_fans));
        }

        @Override
        public void onBindHolder(RecyclerView.ViewHolder holder, final int position) {
            final VH viewHolder = (VH) holder;
            final FollowPersonBean followPersonBean = getData().get(position);
            if (!TextUtils.isEmpty(followPersonBean.getPortrait()))
                IML.load(getContext(), viewHolder.mCirHead, followPersonBean.getPortrait());

            if (!TextUtils.isEmpty(followPersonBean.getNick_name()))
                viewHolder.mTvName.setText(followPersonBean.getNick_name());

            viewHolder.mCirHead.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    UserCenterActivity.start(getContext(), followPersonBean.getUser_id() + "");
                }
            });

            if (followPersonBean.getFollow_state() != FollowState.EACH_FOLLOW) {
                viewHolder.mBtnFollow.setText("关注");
                viewHolder.mBtnFollow.setEnabled(true);
                viewHolder.mBtnFollow.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        MyHttpRequest request = new MyHttpRequest(getContext());
                        String url = String.format(UrlCenter.FOLLOW, followPersonBean.getUser_id());
                        UrlParams urlParams = new UrlParams();
                        request.post(url, urlParams, new DataRequestListener<String>(String.class) {
                            @Override
                            public void onStart(Context context, String url) {
                                enableProgress(false);
                                super.onStart(context, url);
                            }

                            @Override
                            public void onSuccess(String o) {
                                super.onSuccess(o);
                                EasyToast.showToast(getContext(), "关注 " + followPersonBean.getNick_name() + " 成功!");
                                viewHolder.mBtnFollow.setText("互相关注");
                                viewHolder.mBtnFollow.setEnabled(false);
                                startLoading();
                            }

                            @Override
                            public void onFailure(Throwable t, int errorNo, String strMsg) {
                                super.onFailure(t, errorNo, strMsg);
                                EasyToast.showToast(getContext(), strMsg);
                            }
                        });
                    }
                });
            } else {
                viewHolder.mBtnFollow.setText("互相关注");
                viewHolder.mBtnFollow.setEnabled(false);
            }
        }

        public class VH extends RecyclerView.ViewHolder {

            @Bind(R.id.mCirHead)
            CircleImageView mCirHead;
            @Bind(R.id.mTvName)
            TextView mTvName;
            @Bind(R.id.mBtnFollow)
            Button mBtnFollow;

            public VH(View itemView) {
                super(itemView);
                ButterKnife.bind(this, itemView);
            }
        }
    }
}
